package com.easy.sk.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.easy.common.bean.SeckillGoods;
import com.easy.common.service.ISeckillGoodsService;
import com.easy.sk.system.mapper.SeckillGoodsMapper;
//import org.apache.dubbo.config.annotation.DubboService;
import com.easy.sk.system.mapper.SeckillGoodsMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@DubboService
public class SeckillGoodsServiceImpl extends ServiceImpl<SeckillGoodsMapper, SeckillGoods> implements ISeckillGoodsService {
    @Override
    public SeckillGoods getSeckillGoodsById(int id) {
        return super.getOne(new QueryWrapper<SeckillGoods>().eq("id", id));
    }

    @Override
    public SeckillGoods getSeckillGoodsByGoodsId(int goodsId) {
        return super.getOne(new QueryWrapper<SeckillGoods>().eq("goods_id", goodsId));
    }

    @Override
    public int addSeckillGoods(SeckillGoods seckillGoods) {
        // 确保状态默认为0(未开始)
        if (seckillGoods.getStatus() == null) {
            seckillGoods.setStatus(0);
        }
        // 确保删除状态默认为0(未删除)
        if (seckillGoods.getIs_del() == null) {
            seckillGoods.setIs_del(0);
        }
        return super.save(seckillGoods) ? 1 : 0;
    }

    @Override
    public int updateSeckillGoods(SeckillGoods seckillGoods) {
        seckillGoods.setUpdate_time(LocalDateTime.now());
        return super.updateById(seckillGoods) ? 1 : 0;
    }

    @Override
    public int deleteSeckillGoods(Integer id) {
        // 软删除，将is_del字段置为1
        return super.update().set("is_del", 1).eq("id", id).update() ? 1 : 0;
    }

    @Override
    public int reverseStatus(Integer id) {
        SeckillGoods seckillGoods = super.getById(id);
        if (seckillGoods != null) {
            seckillGoods.setStatus(seckillGoods.getStatus() == 1 ? 0 : 1);
        }
        return super.updateById(seckillGoods) ? 1 : 0;
    }

    @Override
    public List<SeckillGoods> getAll(SeckillGoods seckillGoods) {
        LambdaQueryWrapper<SeckillGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillGoods::getIs_del, 0);
        return super.list(queryWrapper);
    }

    @Override
    public IPage<SeckillGoods> getPage(SeckillGoods seckillGoods, IPage page) {
        LambdaQueryWrapper<SeckillGoods> queryWrapper = new LambdaQueryWrapper<>();
        // 添加条件查询支持
        if (seckillGoods.getGoods_id() != null && seckillGoods.getGoods_id() != 0) {
            queryWrapper.eq(SeckillGoods::getGoods_id, seckillGoods.getGoods_id());
        }
        if (seckillGoods.getActivity_id() != null && seckillGoods.getActivity_id() != 0) {
            queryWrapper.eq(SeckillGoods::getActivity_id, seckillGoods.getActivity_id());
        }
        queryWrapper.eq(SeckillGoods::getIs_del, 0);
        return super.page(page, queryWrapper);
    }
}
